<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" ref="queryRef" label-width="80px">
      <el-form-item label="编码">
        <el-input v-model="queryParams.feeCode" placeholder="编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="queryParams.feeName" placeholder="名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['jiaxiao:feeType:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['jiaxiao:feeType:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete()" v-hasPermi="['jiaxiao:feeType:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" :row-key="getRowId">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="feeTypeId" width="100" />
      <el-table-column label="编码" prop="feeCode" width="160" />
      <el-table-column label="名称" prop="feeName" />
      <el-table-column label="默认金额" prop="defaultAmount" width="140" />
      <el-table-column label="状态" prop="status" width="120" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="formRef" :model="form" label-width="100px">
        <el-form-item label="编码"><el-input v-model="form.feeCode" /></el-form-item>
        <el-form-item label="名称"><el-input v-model="form.feeName" /></el-form-item>
        <el-form-item label="默认金额"><el-input-number v-model="form.defaultAmount" :min="0" :step="50" /></el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status"><el-option label="正常" value="0" /><el-option label="停用" value="1" /></el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open=false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeeType, addFeeType, updateFeeType, delFeeType } from '@/api/jiaxiao/feeType'

export default {
  name: 'JxFeeType',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      single: true,
      multiple: true,
      ids: [],
      selectedRows: [],
      queryParams: { pageNum: 1, pageSize: 10, feeCode: undefined, feeName: undefined },
      title: '',
      open: false,
      form: { status: '0' }
    }
  },
  created() { this.getList() },
  methods: {
    getRowId(row){ return row && (row.feeTypeId || row.id || row.feeTypeID || row.FEE_TYPE_ID) },
    getList() {
      this.loading = true
      listFeeType(this.queryParams).then(res => {
        const rows = res.rows || []
        this.list = rows.map(r => ({ ...r, id: r.feeTypeId }))
        this.total = res.total
        this.loading = false
      })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.$refs.queryRef && this.$refs.queryRef.resetFields(); this.handleQuery() },
    handleSelectionChange(sel) {
      try { console.log('[feeType] selection-change selection=', sel) } catch(e) {}
      this.selectedRows = sel
      this.ids = (sel||[]).map(i => this.getRowId(i)).filter(v => v !== undefined && v !== null && v !== '')
      try { console.log('[feeType] mapped ids=', this.ids) } catch(e) {}
      this.single = sel.length !== 1
      this.multiple = !sel.length
    },
    handleAdd() { this.title = '新增类型'; this.form = { status: '0' }; this.open = true },
    handleUpdate(row) { const id = row ? row.feeTypeId : this.ids[0]; const cur = this.list.find(x=>x.feeTypeId===id) || {}; this.form = { ...cur }; this.title = '编辑类型'; this.open = true },
    submitForm() { const req = this.form.feeTypeId ? updateFeeType(this.form) : addFeeType(this.form); req.then(()=>{ this.$message.success('保存成功'); this.open=false; this.getList() }) },
    handleDelete(row) {
      try { console.log('[feeType] handleDelete row=', row, 'ids(state)=', this.ids, 'selectedRows=', this.selectedRows) } catch(e) {}
      const ids = row ? [this.getRowId(row)] : (this.ids && this.ids.length ? this.ids : (this.selectedRows || []).map(r => this.getRowId(r)))
      const idsStr = (ids || []).filter(Boolean).join(',')
      try { console.log('[feeType] computed idsStr=', idsStr) } catch(e) {}
      if (!idsStr) { this.$message.warning('请选择要删除的记录'); return }
      this.$modal.confirm('确认删除选中记录吗？')
        .then(() => delFeeType(idsStr))
        .then(()=>{ this.$message.success('删除成功'); this.getList() })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
</style>


